package com.ch.playandroid.fragment

import android.util.Log
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.ch.common.base.fragment.BaseFragment
import com.ch.playandroid.BR
import com.ch.playandroid.R
import com.ch.playandroid.adapter.AppBannerAdapter
import com.ch.playandroid.adapter.ArticleBindAdapter
import com.ch.playandroid.databinding.FragmentAppBinding
import com.ch.playandroid.viewmodel.AppFragmentViewModel
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.config.IndicatorConfig
import com.youth.banner.indicator.CircleIndicator

class AppFragment : BaseFragment<AppFragmentViewModel, FragmentAppBinding>(),
    OnRefreshLoadMoreListener {
    override val viewModel: AppFragmentViewModel by viewModels()
    private lateinit var mAppBannerAdapter: AppBannerAdapter
    private lateinit var mArticleAdapter: ArticleBindAdapter
    private var curPagerNumber = 0
    private var isLoadMore = true


    override fun getLayoutResID(): Int {
        return R.layout.fragment_app
    }

    override fun initView() {
        initBanner()
        initRecycleView()
        viewModel.getArticleDataList(curPagerNumber)
    }

    private fun initBanner() {
        mAppBannerAdapter = AppBannerAdapter(R.layout.banner_item_layout, viewModel.bannerData)
        val indicator = CircleIndicator(activity)
        indicator.indicatorConfig.gravity = IndicatorConfig.Direction.RIGHT
        indicator.indicatorConfig.normalColor = R.color.app_colorPrimary
        indicator.indicatorConfig.selectedColor = R.color.app_colorError
        mBinding.appBanner.addBannerLifecycleObserver(this)
            .setAdapter(mAppBannerAdapter).indicator = indicator
    }


    private fun initRecycleView() {
        mBinding.recyclerView.layoutManager = LinearLayoutManager(activity)
        mBinding.smartRefreshLayout.setOnRefreshLoadMoreListener(this)
        //传统写法
//        mArticleAdapter =
//            ArticleAdapter(R.layout.article_item_layout, viewModel.mArticleDataList).also {
//                mBinding.recyclerView.adapter = it
//            }
        //databind写法
        mArticleAdapter = ArticleBindAdapter(viewModel.mArticleDataList, BR.dataBean).also {
            mBinding.recyclerView.adapter = it
        }
    }


    override fun initObserve() {
        viewModel.run {
            getBannerData().observe(this@AppFragment) {
                val bannerBean = it.getOrNull()
                bannerBean?.let { item ->
                    bannerData.clear()
                    bannerData.addAll(item.data)
                    mAppBannerAdapter.notifyDataSetChanged()
                }

            }
            getArticleData().observe(this@AppFragment) {
                if (isLoadMore) {
                    mBinding.smartRefreshLayout.finishLoadMore()
                } else {
                    viewModel.mArticleDataList.clear()
                    mBinding.smartRefreshLayout.finishRefresh()
                }
                if (it.isSuccess) {
                    val dataBean = it.getOrNull()
                    dataBean?.run {
                        viewModel.mArticleDataList.addAll(articleData.dataList)
                        mArticleAdapter.notifyDataSetChanged()
                    }
                }

            }
            uiNavigationData.observe(this@AppFragment) {
                ARouter.getInstance().build(it).navigation()
            }
        }
    }


    override fun bindViewModel() {
        mBinding.viewmodel = viewModel
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isLoadMore = false
        curPagerNumber = 0
        viewModel.getArticleDataList(curPagerNumber)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        isLoadMore = true
        ++curPagerNumber
        viewModel.getArticleDataList(curPagerNumber)
    }
}